﻿<?php
/*******************************
 * 留言本数据写入函数
 * @author Erik
 * @author jindw
 * @version 1.0
 * @since 2008-03-16
 *******************************/
/**
 * 写入文件缓存
 * @param $url 缓存文件路径
 * @param $content 缓存文件内容
 */
$cacheDir = realpath(".");

$maxPage = 3;

/**
 * 存在安全问题
 */
function requireMappedDir($url){
    global $cacheDir;
    $file = urlencode($url);
    $hash = preg_replace("/^(.)(.)/","$1/$1$2/$1$2",base_convert(crc32($file),10,32));
    $file = "$cacheDir/data/$hash/$file";
    if(!file_exists($file)){
        //安全问题
        mkdir($file, 0777,true);
    }
    return $file;
}
function writeToCache ($file, $content) {
    //echo realpath($file);
    $toFile = fopen($file, 'w+');
    $lockState = flock($toFile,LOCK_EX);
    fwrite($toFile, $content);
    fclose($toFile);
}
/**
 * 读取文件内容
 * @param $url 读取文件路径
 */
function readFromCache($file) {
    if(file_exists($file)){
        $buff = "";
        $fp = fopen($file, "r") or die;
        $lockState = flock($fp,LOCK_SH);
        while (!feof($fp)) {
            $buff .= fgets($fp,4096);
        }
        fclose($fp);
        return $buff;
    }else{
        return null;
    }
}

/**
{
  pages:[10,100,100],
  list:[{"name":"jindw","email":"test@xidea.org","content":"hello"},.....]
}
 * 数据代理基类
 * @author Erik
 * @version 1.0
 * @since 2008-03-16
 */
class MessageDao {
    var $cacheDir;
    function MessageDao ($url) {
        $this->cacheDir = requireMappedDir($url);
    }
    function get($pageIndex){
        return readFromCache($this->cacheDir."/$pageIndex.js");
    }
    function query($pageIndex,$pageSize) {
        $page0 = readFromCache($this->cacheDir."/0.js");
        if($page0){
            $page0 = json_decode($page0);
            $begin = $pageIndex * $pageSize;
            $end = $begin+$pageSize;
            $pages = $page0->pages;
            
            if($end > $pages[0] && (($count = count(pages)>1)) ){
                $result = 0;
                $j = 0;
                for ($i = 1; $i < $count; $i++) {
                    $items = readFromCache($this->cacheDir."/$i.js");
                }
            }else{
            }
        }
        return array();
    }
    function append($item) {
        global $maxPage;
        $file = $this->cacheDir."/0.js";
        $page0 = readFromCache($file);
        if($page0){
            $page0 = json_decode($page0,true);
        }else{
            $page0 = array(
                "pages"=>array(0),
                "list"=>array()
            );
        }
        $pages = &$page0["pages"];
        $list = &$page0["list"];
        array_push($list, $item);
        $count = count($list);
        if($count>$maxPage + $maxPage/2){
            $pages[0] = $maxPage;
            $content = json_encode(array_splice($list, 0, $maxPage));
            $pageCount = count($pages);
            writeToCache ($this->cacheDir."/$pageCount.js", $content);
            array_push($pages, $maxPage);
            $count -= $maxPage;
        }
        $pages[0] = $count;
        $content = json_encode($page0);
        writeToCache ($file, $content);
        return $content;
    }
}
?><?php
/*******************************
 * @author jindw
 * @version 1.0
 * @since 2008-03-16
 *******************************/
header("Content-Type:text/html;charset=utf8");
$msgDao = new MessageDao($_SERVER["HTTP_REFERER"]);
if($_SERVER["REQUEST_METHOD"] == "GET"){
    if(array_key_exists("pageIndex",$_GET)){
        $pageIndex = $_GET["pageIndex"];
    }else{
        $pageIndex = 0;
    }
    $result = $msgDao->get($pageIndex);
    if(array_key_exists("callback",$_GET)){
       echo $_GET["callback"];
    }
    echo "(".$result.")";
}else{
    $data = array();
    foreach($_GET as $key => $value){
        if(!strncmp($key,"message.",7)){
            $data[substr($key,8)] = $value;
        }
    }
	$msgDao->append($data);
}

//echo $msgDao->getContent();
?>